<template>
    <view class="container">
  <scroll-view scroll-y="true" class="kline-list">
    <view class="kline-item kline-header">
      <text class="cell time">时间</text>
      <text class="cell price">开盘</text>
      <text class="cell price">收盘</text>
      <text class="cell price">最高</text>
      <text class="cell price">最低</text>
      <text class="cell volume">成交量</text>
      <!--
      <text class="cell amount">成交额</text>
    -->
    </view>

    <view class="kline-item" v-for="(k, index) in klines" :key="index">
      <text class="cell time">{{ k.time }}</text>
      <text class="cell price">{{ k.open.toFixed(2) }}</text>
      <text class="cell price">{{ k.close.toFixed(2) }}</text>
      <text class="cell price">{{ k.high.toFixed(2) }}</text>
      <text class="cell price">{{ k.low.toFixed(2) }}</text>
      <text class="cell volume">{{ k.volume }}</text>
      <!--
        <text class="cell amount">{{ k.amount.toFixed(2) }}</text>
      -->
    </view>
  </scroll-view>
</view>

</template>
<script>
export default {
    data() {
      return {
          code: '', // 用来保存传入的股票代码
          klines:[]
          }
    },
  // 页面生命周期函数，接收跳转时的参数
    onLoad(options) {
        //this.getKlineData(options).then(data => {console.log('600000 最新20根5分钟K线:', data);});
        this.getKlineData(options.code);
    },
    methods: {
        // 这里放你页面的方法
        async getKlineData(code) {
            const res  = await uni.request({
                 url: `https://push2his.eastmoney.com/api/qt/stock/kline/get`,
                 data: { secid: `${code}`, fields1: 'f1,f2,f3,f4,f5,f6', fields2: 'f51,f52,f53,f54,f55,f56,f57', klt: 5, fqt: 1, end: '20500101', lmt: 20 },
            });
            const klines = res.data?.data?.klines || [];
            const parsed = klines.map(item => {
            const [rawTime, open, close, high, low, volume, amount] = item.split(',');
            const time = rawTime.split(' ')[1]; // "2025-09-11 10:15" -> "10:15"
            return {
                time,                        // 时间
                open: parseFloat(open),      // 开盘价
                close: parseFloat(close),    // 收盘价
                high: parseFloat(high),      // 最高价
                low: parseFloat(low),        // 最低价
                volume: parseInt(volume),    // 成交量
                amount: parseFloat(amount)   // 成交额
            };
            });
            this.klines = parsed;
        }
    }
}
</script>

<style>
    .container {
  padding: 10px;
  background-color: #f5f5f5;
  height: 100%;
}

.kline-list {
  background-color: #fff;
  border-radius: 8px;
  overflow: hidden;
  height: 100%;
}

.kline-item {
  display: flex;
  flex-direction: row;
  padding: 2px 4px;
  border-bottom: 1px solid #eee;
  align-items: center;
}

.kline-header {
  background-color: #007aff;
  color: #fff;
  font-weight: bold;
}

.cell {
  flex: 1;
  text-align: center;
  font-size: 12px;
}

.time { flex: 1; }
.price { flex: 1; }
.volume { flex: 1; }
.amount { flex: 1; }

/* 选中高亮样式（可选） */
.kline-item:hover {
  background-color: #f0f8ff;
}

</style>